package com.china08.yxyapi.api.manage.edu.office.outernotice;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter.YxyNotifictionSchool;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeOutterRepository;

/**
 * 教育局行政办公 外部通知 删除通知
 * 
 */
@CrossOrigin
@RestController("OuterNoticeReceiveDelController")
public class OuterNoticeReceiveDelController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeOutterRepository yxyNoticeOutterRepository;

    @RequestMapping(value = "/api_yw/manage/edu/office/outerNotice/receive/del", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel//
    ) {

        RespModel respModel = new RespModel();
        // 当前机构
        String edbId = getProfile(request, response).getSchoolId();
        // 当前用户
        // String currentUserId = getProfile(request, response).getId();
        // 外部通知id
        String ids = reqModel.ids;
        List<String> idLst = Arrays.asList(ids.split(","));
        // 外部通知是否删除
        int isDelete = 0;
        List<YxyNoticeOutter> yxyNoticeOutterLst = yxyNoticeOutterRepository
                .findByIdInAndReceiveSchoolSchoolIdAndReceiveSchoolIsDelete(idLst, edbId, isDelete);

        for (YxyNoticeOutter yxyNoticeOutter : yxyNoticeOutterLst) {
            for (YxyNotifictionSchool yxyNotifictionSchool : yxyNoticeOutter.getReceiveSchool()) {
                if (yxyNotifictionSchool.getIsDelete() == 0 && yxyNotifictionSchool.getSchoolId().equals(edbId)) {
                    yxyNotifictionSchool.setIsDelete(-1);
                }
            }
        }
        // 更改为删除状态
        yxyNoticeOutterRepository.save(yxyNoticeOutterLst);
        respModel.setMessage("删除成功");
        return respModel;
    }

    public class ReqModel {
        // 通知id
        public String ids;

        public String getIds() {
            return ids;
        }

        public void setIds(String ids) {
            this.ids = ids;
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}